package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.itheima.constant.MessageConstant.ORDERSETTING_FAIL;

@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {
    @Reference(version = "1.0")
    OrderSettingService orderSettingService;

    @PostMapping("upload")
    public Result upload(@RequestParam("excelFile") MultipartFile file){
        List<String[]> readExcel = null;
        try {
            readExcel = POIUtil.readExcel(file);
            ArrayList<OrderSetting> orderList = new ArrayList<>();
            for (String[] strings : readExcel) {
                Date orderDate = new Date(strings[0]);

                Integer number = Integer.parseInt(strings[1]);
                OrderSetting orderSetting = new OrderSetting();

                orderSetting.setOrderDate(orderDate);
                orderSetting.setNumber(number);
                orderSetting.setReservations(0);

                orderList.add(orderSetting);
            }
            return orderSettingService.save(orderList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.fail(500,ORDERSETTING_FAIL);
    }

    @GetMapping("findByMonth")
    public Result findByMonth(String month){
        return orderSettingService.findByMonth(month);
    }

    @GetMapping("updateNumber")
    public Result updateNumber(String date,Integer number){
        return orderSettingService.updateNumber(date,number );
    }
}
